package com.ruoyi.web.erp.controller;

import com.czkt.domain.ErpProductAttrs;
import com.czkt.domain.vo.SpuExcelVo;
import com.czkt.service.IErpProductAttrsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.czkt.domain.ErpSpu;
import com.czkt.service.IErpSpuService;
import com.czkt.domain.vo.SpuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 主商品Controller
 * 
 * @author ruoyi
 * @date 2024-04-26
 */
@RestController
@RequestMapping("/system/spu")
@Api("sku管理")
public class ErpSpuController extends BaseController
{
    @Autowired
    private IErpSpuService erpSpuService;

    /**
     * 查询主商品列表
     */
    @PreAuthorize("@ss.hasPermi('system:spu:list')")
    @GetMapping("/list")
    public TableDataInfo list(ErpSpu erpSpu)
    {
        startPage();
        List<ErpSpu> list = erpSpuService.selectErpSpuList(erpSpu);
        return getDataTable(list);
    }

    /**
     * 导出主商品列表
     */
    @PreAuthorize("@ss.hasPermi('system:spu:export')")
    @Log(title = "主商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpSpu erpSpu)
    {
        List<SpuExcelVo> list = erpSpuService.selectSpuExcelVoList(erpSpu);
        ExcelUtil<SpuExcelVo> util = new ExcelUtil<SpuExcelVo>(SpuExcelVo.class);
        util.exportExcel(response, list, "主商品数据");
    }

    /**
     * 获取主商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:spu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(erpSpuService.selectErpSpuById(id));
    }

    /**
     * 新增主商品
     */
    @PreAuthorize("@ss.hasPermi('system:spu:add')")
    @Log(title = "主商品", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增主商品")
    public AjaxResult add(@RequestBody SpuVo erpSpu )
    {
        return toAjax(erpSpuService.insertErpSpu(erpSpu,erpSpu.getSkuList(),erpSpu.getAttrValues()));
    }

    /**
     * 修改主商品
     */
    @PreAuthorize("@ss.hasPermi('system:spu:edit')")
    @Log(title = "主商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpSpu erpSpu)
    {
        return toAjax(erpSpuService.updateErpSpu(erpSpu));
    }

    /**
     * 删除主商品
     */
    @PreAuthorize("@ss.hasPermi('system:spu:remove')")
    @Log(title = "主商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(erpSpuService.deleteErpSpuByIds(ids));
    }

    @Autowired
    IErpProductAttrsService erpProductAttrsService;

    /**
     * 获取商品属性
     */
    @PreAuthorize("@ss.hasPermi('system:spu:getAttr')")
    @Log(title = "主商品", businessType = BusinessType.UPDATE)
    @GetMapping("/attrlist")
    public TableDataInfo getProductAttr()
    {
        return getDataTable(erpProductAttrsService.selectErpProductAttrsList(new ErpProductAttrs()));
    }


}
